package com.mldong.modules.sys.service.impl;

import cn.hutool.Hutool;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mldong.base.CommonPage;
import com.mldong.modules.sys.dto.FileInfoPageParam;
import com.mldong.modules.sys.dto.FileInfoParam;
import com.mldong.modules.sys.vo.FileInfoVO;
import org.dromara.x.file.storage.core.recorder.FileRecorder;
import org.dromara.x.file.storage.core.upload.FilePartInfo;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import com.mldong.modules.sys.entity.FileInfo;
import com.mldong.modules.sys.mapper.FileInfoMapper;
import com.mldong.modules.sys.service.FileInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 文件信息 服务实现类
 * </p>
 *
 * @author mldong
 * @since 2022-10-23
 */
@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService, FileRecorder {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(FileInfoParam param) {
        param.setId(null);
        FileInfo FileInfo = new FileInfo();
        BeanUtil.copyProperties(param, FileInfo);
        return super.save(FileInfo);
    }

    @Override
    public boolean update(FileInfoParam param) {
        FileInfo FileInfo = new FileInfo();
        BeanUtil.copyProperties(param, FileInfo);
        return super.updateById(FileInfo);
    }

    @Override
    public CommonPage<FileInfoVO> page(FileInfoPageParam param) {
        IPage<FileInfoVO> page = param.buildMpPage();
        QueryWrapper queryWrapper = param.buildQueryWrapper();
        List<FileInfoVO> list = baseMapper.selectCustom(page, queryWrapper);
        page.setRecords(list);
        return CommonPage.toPage(page);
    }
    @Override
    public FileInfoVO findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public boolean save(org.dromara.x.file.storage.core.FileInfo fileInfo) {
        if(!ObjectUtil.equals(fileInfo.getAttr().getInt("persist"),1))return true;
        FileInfo info = new FileInfo();
        BeanUtil.copyProperties(fileInfo, info,"id","attr");
        info.setAttr(JSONUtil.toJsonStr(fileInfo.getAttr()));
        info.setId(Convert.toLong(fileInfo.getObjectId()));
        String url = StrUtil.format("{}{}{}",
                fileInfo.getBasePath(),
                fileInfo.getPath(),
                fileInfo.getFilename());
        info.setUrl(url);
        //计算文件大小信息
        if(info.getSize()!=null) {
            info.setSizeInfo(FileUtil.readableFileSize(info.getSize()));
        }
        if(info.getThSize()!=null) {
            info.setThSizeInfo(FileUtil.readableFileSize(info.getThSize()));
        }
        baseMapper.insert(info);
        return true;
    }

    @Override
    public void update(org.dromara.x.file.storage.core.FileInfo fileInfo) {

    }

    @Override
    public org.dromara.x.file.storage.core.FileInfo getByUrl(String s) {
        return null;
    }

    @Override
    public boolean delete(String s) {
        return false;
    }

    @Override
    public void saveFilePart(FilePartInfo filePartInfo) {

    }

    @Override
    public void deleteFilePartByUploadId(String s) {

    }
}
